package org.openharmony.qttool;

import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.util.ui.JBUI;
import org.openharmony.qttool.classgenerator.QtClassGenerator;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

public class NewQtClassDialog extends DialogWrapper {

    private JTextField className;
    private ComboBox<String> baseName;

    private final String saveTo;

    protected NewQtClassDialog(String saveTo) {
        super(null, null, true, IdeModalityType.MODELESS, true);
        init();
        setTitle("New Qt C++ Class");
        this.saveTo = saveTo;
    }

    @Override
    protected JComponent createCenterPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = JBUI.insets(5);

        // add class name
        constraints.gridx = 0;
        constraints.gridy = 0;
        panel.add(new JLabel("Class Name:"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.weightx = 1.0; // 设置水平权重
        className = new JTextField();
        AbstractDocument doc = (AbstractDocument) className.getDocument();
        doc.setDocumentFilter(new ClassNameDocumentFilter());
        panel.add(className, constraints);

        constraints.gridx = 0;
        constraints.gridy = 1;
        panel.add(new JLabel("Base Name:"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.weightx = 1.0; // 设置权重
        baseName = new ComboBox<>(new String[]{"QObject", "QWidget", "QMainWindow"});
        panel.add(baseName, constraints);
        return panel;
    }

    @Override
    protected ValidationInfo doValidate() {
        if (className.getText().isEmpty()) {
            ValidationInfo info = new ValidationInfo("The class name is empty", className);
            info.okEnabled = false;
            return info;
        }
        return super.doValidate();
    }

    @Override
    protected void doOKAction() {
        QtClassGenerator g = new QtClassGenerator(className.getText(), (String) baseName.getSelectedItem(), saveTo);
        if (!g.generate()) {
            Messages.showErrorDialog("Generate class failed", "Error");
        }
        super.doOKAction();
    }

    private static class ClassNameDocumentFilter extends DocumentFilter {
        @Override
        public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
            if (isValidClassName(fb.getDocument().getText(0, fb.getDocument().getLength()) + string)) {
                super.insertString(fb, offset, string, attr);
            }
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            if (isValidClassName(fb.getDocument().getText(0, fb.getDocument().getLength()) + text)) {
                super.replace(fb, offset, length, text, attrs);
            }
        }

        private boolean isValidClassName(String className) {
            return className.matches("[a-zA-Z_][a-zA-Z0-9_]*");
        }
    }
}